package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//菜品模块
@RestController
public class DishController {

    @Autowired
    private DishService dishService;

    @GetMapping("/dish/page")
    public ResultInfo findByPage(
            @RequestParam (name = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize,String name){

        //调用service
        Page<Dish> page = dishService.findByPage(pageNum,pageSize,name);
        //返回结果
        return ResultInfo.success(page);
    }

    //新增菜品
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish){//1.接收post请求参数
        //2.调用service
        dishService.save(dish);
        //3.返回成功
        return ResultInfo.success();
    }

    //回显菜品
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable Long id){//1.接收路径参数
        //2.调用service查询
        Dish dish = dishService.findById(id);
        //3.返回结果
        return ResultInfo.success(dish);

    }


    //修改信息
    @PutMapping("/dish")
    public ResultInfo updateById(@RequestBody Dish dish){
        //2.调用service修改
        dishService.updateById(dish);
        //3.返回结果
        return ResultInfo.success();
    }

    //停售
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStatus(@PathVariable Integer status,@RequestParam List<Long>ids){
        //调用service
        dishService.updateStatus(status,ids);

        //返回结果
        return ResultInfo.success();
    }

    //删除
    @DeleteMapping("/dish")
    public ResultInfo deleteBatchIds(@RequestParam List<Long>ids){
        //调用service
        dishService.deleteBatchIds(ids);
        //返回结果
        return ResultInfo.success();
    }


    //套餐查询
    @GetMapping("/dish/list")
    public ResultInfo findListByCategoryId(Long categoryId,String name){
        //判断
        List<Dish>dishList=null;
        if (categoryId!=null){
            //调用mapper
            dishList = dishService.findListByCategoryId(categoryId);
        }
        if (name!=null){
            //调用mapper
            dishList = dishService.find(name);
        }
        //返回结果
        return ResultInfo.success(dishList);
    }

}
